package com.wyh.controller.g3controller;

import com.wyh.common.result.Result;
import com.wyh.domain.vo.g3vo.ReconciliationVO;
import com.wyh.service.g3service.ReconciliationService;
import com.wyh.utils.g3utils.BillidGenerateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "调账")
@RestController
@RequestMapping("/reconciliation")
public class ReconciliationController {

    @Autowired
    private ReconciliationService reconciliationService;

    @ApiOperation("查找调账信息")
    @GetMapping("/find")
    public Result<List<Map<String, String>>> findReconciliation(Integer id) {
        List<ReconciliationVO> reconciliationList = reconciliationService.findReconciliation(id);
        List<Map<String, String>> resultList = new ArrayList<>();
        for (ReconciliationVO reconciliation : reconciliationList) {
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("id", String.valueOf(reconciliation.getId()));
            resultMap.put("propertyname", reconciliation.getPropertyname());
            resultMap.put("billingitemsname", reconciliation.getBillingitemsname());
            // 处理 billingNatureDesc
            int billingNature = reconciliation.getBillingnature();
            String billingNatureDesc;
            switch (billingNature) {
                case 0:
                    billingNatureDesc = "周期性计费";
                    break;
                case 1:
                    billingNatureDesc = "三表类计费";
                    break;
                case 2:
                    billingNatureDesc = "一次性计费";
                    break;
                default:
                    billingNatureDesc = "未知类型";
            }
            resultMap.put("billingNatureDesc", billingNatureDesc);
            resultMap.put("name", reconciliation.getName());
            // 处理 type
            String typeDesc;
            int type = reconciliation.getType();
            switch (type) {
                case 0:
                    typeDesc = "房屋";
                    break;
                case 1:
                    typeDesc = "车位";
                    break;
                default:
                    typeDesc = "未知类型";
            }
            resultMap.put("type", typeDesc);
            resultMap.put("customername", reconciliation.getCustomername());
            resultMap.put("receivable", String.valueOf(reconciliation.getReceivable()));
            resultList.add(resultMap);
        }
        return Result.success(resultList);
    }


    @ApiOperation("更新应收金额")
    @PutMapping("/updateReceivable")
    public Result<String> updateReceivable(@RequestBody ReconciliationVO reconciliationVO) {
        reconciliationService.updateReceivable(reconciliationVO);
        return Result.success("修改成功");
    }

    @ApiOperation("批量调账")
    @PostMapping("/ReconciliationBatch")
    public Result<String> reconciliationBatch(@RequestBody Map<String, Object> request) {
        Integer id = (Integer) request.get("id");
        BigDecimal receivable = new BigDecimal((String) request.get("receivable"));
        List<String> customernames = (List<String>) request.get("customernames");
        List<String> receivablesAsString = (List<String>) request.get("receivables");

        List<BigDecimal> receivables = new ArrayList<>();
        for (String receivableStr : receivablesAsString) {
            receivables.add(new BigDecimal(receivableStr));
        }

        BigDecimal totalReceivables = BigDecimal.ZERO;
        for (BigDecimal amount : receivables) {
            totalReceivables = totalReceivables.add(amount);
        }

        ReconciliationVO chargingByCid = reconciliationService.findChargingByCid(id);

        if (totalReceivables.compareTo(receivable) > 0) {
            return Result.error("总和超过了 receivable");
        } else {
            BigDecimal newReceivable = receivable.subtract(totalReceivables);
            chargingByCid.setReceivable(newReceivable);
            reconciliationService.updateReceivable(chargingByCid);
        }

        for (int i = 0; i < customernames.size(); i++) {
            String customername = customernames.get(i);
            BigDecimal receivableForCustomer = receivables.get(i);

            if (chargingByCid != null) {
                // 设置新的 receivable
                chargingByCid.setCustomername(customername);
                chargingByCid.setReceivable(receivableForCustomer);
                chargingByCid.setBillid(Long.parseLong(BillidGenerateUtils.generateFixedLengthIndex()));

                reconciliationService.insertCharging(chargingByCid);
            } else {
                return Result.error("用户不存在");
            }
        }
        return Result.success("数据插入成功");

    }
}